package com.hd.service.activiti.impl;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.hd.dao.BaseDao;
import com.hd.entity.activiti.ProcessInstanceEntity;
import com.hd.entity.sys.User;
import com.hd.service.activiti.ActivitiService;
import com.hd.service.activiti.ProcessInstanceEntityService;
import com.hd.service.sys.UserService;
import com.hd.util.JsonUtil;
import com.hd.util.StringUtils;
import com.hd.util.result.PageInfo;
@Service("processInstanceEntityService")
public class ProcessInstanceEntityServiceImpl implements ProcessInstanceEntityService {
	@Autowired
	private BaseDao<ProcessInstanceEntity,String>processInstanceEntityDao;
	@Autowired
	private ActivitiService activitiService;
	@Autowired
	private UserService userService;
	@Override
	public Serializable save(ProcessInstanceEntity processInstance) {
		return processInstanceEntityDao.save(processInstance);
	}

	@Override
	public void update(ProcessInstanceEntity processInstance) {
		processInstanceEntityDao.update(processInstance);

	}

	@Override
	public void delete(ProcessInstanceEntity processInstance) {
		processInstanceEntityDao.delete(processInstance);
	}

	@Override
	public ProcessInstanceEntity get(String processInstanceId) {
		return processInstanceEntityDao.get(ProcessInstanceEntity.class, processInstanceId);
	}

	@Override
	public void deleteById(String processInstanceId) {
		ProcessInstanceEntity vo = get(processInstanceId);
		if(vo!=null) {
			delete(vo);
		}
	}

	@Override
	public void selectDataGrid(PageInfo info) {
		String hql="from ProcessInstanceEntity where 1=1 ";
		Map<String,Object>condition=info.getCondition();
		List<Object>params=new ArrayList<Object>();
		if(StringUtils.isNotNull(condition.get("title"))) {
			hql+=" and title like ?";
			params.add("%"+condition.get("title")+"%");
		}
		if(StringUtils.isNotNull(condition.get("userName"))) {
			hql+=" and userName like ?";
			params.add("%"+condition.get("userName")+"%");
		}
		if(StringUtils.isNotNull(condition.get("userid"))) {
			hql+=" and userid=?";
			params.add(condition.get("userid"));
		}
		if(StringUtils.isNotNull(condition.get("businessKey"))) {
			hql+=" and businessKey=?";
			params.add(condition.get("businessKey"));
		}
		if(StringUtils.isNotNull(condition.get("processInstanceId"))) {
			hql+=" and processInstanceId=?";
			params.add(condition.get("processInstanceId"));
		}
		if(StringUtils.isNotNull(condition.get("status"))) {
			hql+=" and status=?";
			params.add(condition.get("status"));
		}
		if(StringUtils.isNotNull(condition.get("startTimeStart"))) {
			hql+=" and startTime>=?";
			params.add(condition.get("startTimeStart"));
		}
		if(StringUtils.isNotNull(condition.get("startTimeEnd"))) {
			hql+=" and startTime<=?";
			params.add(condition.get("startTimeEnd"));
		}
		hql+=" order by status asc";
		List<ProcessInstanceEntity>processInstances=processInstanceEntityDao.findPage(hql, info.getNowpage(), info.getPagesize(), params);
		if(processInstances.size()>0) {
			for(ProcessInstanceEntity processInstance:processInstances) {
				if(processInstance.getStatus()==1) {
        			Map<String, Object> map = activitiService.findTodoTaskAssigneeOrCandidateUsers(processInstance.getProcessInstanceId());
        			if(StringUtils.isNotNull(map.get("userNames"))) {
        				String taskAssigneeOrCandidateUsers = (String) map.get("userNames");
        				processInstance.setAssigneeOrCandidateUser(taskAssigneeOrCandidateUsers);
        			}
        			if(StringUtils.isNotNull(map.get("taskName"))) {
        			 processInstance.setTaskName((String)map.get("taskName"));
        			}
        		}
			}
		}
		String countHql="select count(*) "+hql;
		int total=Integer.parseInt(processInstanceEntityDao.getCountByList(countHql,params).toString());
		info.setTotal(total);
		info.setRows(processInstances);
	}

	

}
